package snd;

import java.io.BufferedReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

import bluetoothcomm.TwoWayComm;

public class SndPlayerMbed extends SndPlayer{	
	// New added, reverse order
	
	private static final float maxGain = 12;
	private static final float minGain = -12;
	private float vol = 0;

	private static final String path = "./sndMbed/";
	//private static final String ext  = ".mp3";
	
	private HashMap<String,Integer> sndMap = new HashMap<String,Integer>();
	
	public SndPlayerMbed() {
		// TODO Auto-generated constructor stub
		super();
		sndMap =  genSndMap();
	}
	
	public void increaseVol() {
		TwoWayComm.mySerialWriter.send(169);
		TwoWayComm.mySerialWriter.send(200);
	}
	
	public void decreaseVol() {
		TwoWayComm.mySerialWriter.send(169);
		TwoWayComm.mySerialWriter.send(199);
	}
	
	public HashMap<String,Integer> genSndMap()
	{	
		int i =0;
		HashMap<String,Integer> sndMap = new HashMap<String,Integer>();
		try {
		    BufferedReader in = new BufferedReader(new FileReader("./sndMbed/fileNameList.txt"));
		    String str;
		    while ((str = in.readLine()) != null) {
		    	sndMap.put(str, i++);
		    }
		    in.close();
		} catch (FileNotFoundException e) {
			System.out.println("fileNameList not found");
		} catch (IOException e) {
		}	
		return sndMap;
	}
	
	public int getSongNumber(String fileName)
	{
		System.out.println(fileName);
		return sndMap.get(fileName);
	}
	
	public void printSndMap(HashMap<String,Integer> sndMap)
	{
		System.out.println("Map size : " + sndMap.size());
		System.out.println(sndMap.toString());
	}
	
	public String genFilename(String str) {
		return str;
	}
	
	public int putLine(String strFileName)
	{
		int song = getSongNumber(strFileName);
		TwoWayComm.mySerialWriter.send(169);
		TwoWayComm.mySerialWriter.send(song);
		System.out.println("song number " + song);
		return 1;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Test SndPlayerMbed");
		SndPlayerMbed sndPlayerMbed = new SndPlayerMbed();
		sndPlayerMbed.printSndMap(sndPlayerMbed.sndMap);
		sndPlayerMbed.putLine("checkpoint");
	}

}
